package com.lagou.servlet;

import com.lagou.bean.Student;
import com.lagou.service.StudentService;
import com.lagou.util.JSONUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "StudentServlet", urlPatterns = "/student")
public class StudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求中的学生查询条件信息
        String action = request.getParameter("action");
        System.out.println("操作类型为：" + action);

        //创建UserService类型的对象去实现数据的校验功能
        StudentService studentService = new StudentService();

        //查询学生信息
        if("listAll".equals(action)){
            List<Student> list = studentService.stdQurService();
            System.out.println(list);
            if (null != list) {
                // 返回JSON数据格式
                JSONArray jsonArr = JSONArray.fromObject(list);
                JSONObject jsonObjOut = new JSONObject();
                jsonObjOut.put("list", jsonArr);
                JSONUtil.returnJSON(request, response, jsonObjOut);
            }
        }
        //添加学生信息
        else if("add".equals(action)){
            // 获取JSON数据
            JSONObject jsonObjIn = JSONUtil.getJSON(request, response);

            System.out.println(jsonObjIn.toString());
            Student student = new Student();
            student.setId(Integer.valueOf(jsonObjIn.getString("id")));
            student.setName(jsonObjIn.getString("name"));
            student.setSex(jsonObjIn.getString("sex"));
            student.setBirthday(jsonObjIn.getString("birthday"));
            student.setEmail(jsonObjIn.getString("email"));
            student.setRemark(jsonObjIn.getString("remark"));

            boolean isSuccess = studentService.addStd(student);
            // 返回JSON数据格式
            JSONObject jsonObjOut = new JSONObject();
            jsonObjOut.put("isSuccess", isSuccess);
            JSONUtil.returnJSON(request, response, jsonObjOut);

        }

        //更新学生信息
        else if("update".equals(action)){
            // 获取JSON数据
            JSONObject jsonObjIn = JSONUtil.getJSON(request, response);

            System.out.println(jsonObjIn.toString());
            Student student = new Student();
            student.setId(Integer.valueOf(jsonObjIn.getString("id")));
            student.setName(jsonObjIn.getString("name"));
            student.setSex(jsonObjIn.getString("sex"));
            student.setBirthday(jsonObjIn.getString("birthday"));
            student.setEmail(jsonObjIn.getString("email"));
            student.setRemark(jsonObjIn.getString("remark"));

            boolean isSuccess = studentService.updateStd(student);
            // 返回JSON数据格式
            JSONObject jsonObjOut = new JSONObject();
            jsonObjOut.put("isSuccess", isSuccess);
            JSONUtil.returnJSON(request, response, jsonObjOut);
        }

        //删除学生信息
        else if("delete".equals(action)) {
            // 获取JSON数据
            JSONObject jsonObjIn = JSONUtil.getJSON(request, response);

            Student student = new Student();
            student.setId(Integer.valueOf(jsonObjIn.getString("id")));

            boolean isSuccess = studentService.delStd(student);
            // 返回JSON数据格式
            JSONObject jsonObjOut = new JSONObject();
            jsonObjOut.put("isSuccess", isSuccess);
            JSONUtil.returnJSON(request, response, jsonObjOut);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
